﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class CodeIncrementController : MyControllerBase
    {
        CodeIncrementManager manager;
        CodeIncrementFetcher fetcher;
        public CodeIncrementController() : base()
        {
            this.manager = new CodeIncrementManager();
            this.fetcher = new CodeIncrementFetcher();
        }
        
        [HttpGet]
        [Authorize]
        public AsmObject<String> GetCode(string type)
        {
            LogHelper.Debug(this.GetType(), String.Format("GETCODE:[RQS|{1}]{0}", Util.ConvertToJson(type), RequestIp));
            var code = fetcher.GetByType(type);
            if (code == null)
            {
                code.Type = type;
                code.Seq = 0;
            }
            code.Seq += 1;
            var result = new AsmObject<String>();
            result.Data = String.Format("{0}{1}{2}",type,DateTime.Today.ToString("yyMMdd"),(10000+code.Seq).ToString().Substring(1,4));
            result.Message = "操作成功";
            result.Result = AmsCode.Succ;
            if (code.Seq >= 9999)
                code.Seq = 1;
            manager.Set(code);
            DBHelper.Commit(true);
            LogHelper.Debug(this.GetType(), String.Format("GETCODE:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}